package zdh.avatar.gui;

import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import zdh.avatar.*;
import zdh.avatar.base.*;
/**
 *
 * @author Gem Seeker
 */
public class EditPanelButton extends javax.swing.JFrame implements ActionListener {

    public static final byte PANELS_MODE = 0x0;
    public static final byte BUTTONS_MODE = 0x1;
    public static final byte SET_CHILD_MODE = 0x2;
    private final byte mode;
    
    private final MainFrame main;
    private final AvatarConfig config;
    
    private ElementPanel[] panels;
    private ElementButton[] buttons;
    
    public EditPanelButton(MainFrame main, AvatarConfig config, byte mode) {
        this.main = main;
        this.config = config;
        this.mode = mode;
        
        initComponents();
        setComponents();
        initListeners();
        
        this.getRootPane().setDefaultButton(btn_ok);
    }
    
    private void setComponents(){
        if(mode == PANELS_MODE){
            panels = ElementLoader.getElementPanels(config.getElementsPath() + "panels/");
            if(panels != null){
                SwingUtilities.invokeLater(new Runnable(){
                    @Override public void run(){
                        list.setModel(new ModelPanelList(panels));
                        list.repaint();
                    }
                });
            }
        }else{
            buttons = ElementLoader.getElementButtons(config.getElementsPath() + "buttons/");
            if(buttons != null){
                SwingUtilities.invokeLater(new Runnable(){
                    @Override public void run(){
                        list.setModel(new ModelButtonList(buttons));
                        list.repaint();
                    }
                });
            }
        }
    }

    private void initListeners(){
        btn_ok.addActionListener(this);
        btn_cancel.addActionListener(this);
        
        this.addWindowListener(new WindowAdapter(){
            @Override public void windowClosing(WindowEvent e){
                main.setEnabled(true);
            }
        });
    }
    
    @Override public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        
        if(source == btn_ok){
            if(list.isSelectionEmpty()){
                JOptionPane.showMessageDialog(this, "No item selected.");
            }else{
                if(mode == PANELS_MODE){
                    String s = list.getSelectedValue().toString();
                    ElementPanel p = null;
                    for(ElementPanel _p: panels){
                        if(_p.getPanelName().equals(s)){
                            p = _p;
                            break;
                        }
                    }
                    
                    if(p != null){
                        new AddEditGroup(main, config.getElementsPath(), 
                                AddEditGroup.EDIT, p).showWindow();
                    }
                }else{
                    String s = list.getSelectedValue().toString();
                    ElementButton b = null;
                    for(ElementButton _b: buttons){
                        if(_b.getButtonName().equals(s)){
                            b = _b;
                            break;
                        }
                    }
                    
                    if(b != null){
                        if(mode == BUTTONS_MODE){
                            new AddEditButton(main, config, 
                                AddEditButton.EDIT, b).showWindow();
                        }else{
                            new SetButtonChild(main, config, b).showWindow();
                        }
                    }
                }
                closeWindow();
            }
        }
        
        else if(source == btn_cancel){
            closeWindow();
        }
    }
    
    public void showWindow(){
        SwingUtilities.invokeLater(new Runnable(){
            @Override public void run(){
                main.setEnabled(false);
                setLocationRelativeTo(null);
                setVisible(true);
            }
        });
    }
    
    private void closeWindow(){
        main.setEnabled(true);
        dispose();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JList();
        label = new javax.swing.JLabel();
        btn_cancel = new javax.swing.JButton();
        btn_ok = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select Item to Edit");
        setResizable(false);

        list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(list);

        label.setText("Select the item to edit:");

        btn_cancel.setText("Cancel");

        btn_ok.setText("OK");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btn_cancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_cancel)
                    .addComponent(btn_ok))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_cancel;
    private javax.swing.JButton btn_ok;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label;
    private javax.swing.JList list;
    // End of variables declaration//GEN-END:variables

}
